package com.nbst.service.base.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.nbst.comnutil.LogOut;
import com.nbst.comnutil.NormalResult;
import com.nbst.comnutil.StringUtil;
import com.nbst.comnutil.Tools;
import com.nbst.dao.mapper.mes.DataBaseMapper;
import com.nbst.model.base.DataBase;
import com.nbst.service.base.IDataBaseService;

@Service
public class DataBaseServiceImpl implements IDataBaseService {
	@Autowired
	DataBaseMapper basesmapper;

	@Autowired
	LogOut logout;

	@Override
	public NormalResult deleteByPrimaryKey(String id) {
		NormalResult result = new NormalResult("9999", "操作失败");
		try {

			basesmapper.deleteByPrimaryKey(id);
			result.setCode("0000");
			result.setMessage("操作成功");
		} catch (Exception e) {
			result.setCode("9999");
			result.setDataset(e);
			result.setMessage("删除失败");
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();// 手动回滚
		}
		return result;
	}

	@Override
	public NormalResult insertSelective(DataBase record) {
		NormalResult result = new NormalResult("9999", "操作失败");
		try {
			record.setCjsj(new Date());
			record.setSfyx("1");
			basesmapper.insertSelective(record);
			result.setCode("0000");
			result.setMessage("操作成功");
		} catch (Exception e) {
			result.setCode("9999");
			result.setDataset(e);
			result.setMessage("新增失败");
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();// 手动回滚
		}
		return result;
	}

	@Override
	public List<DataBase> findByCondition(Map<String,Object> map) {
		List<DataBase> list = new ArrayList<DataBase>();
		try {
			list = basesmapper.findByCondition(map);
		} catch (Exception e) {
			logout.error(e);
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();// 手动回滚
		}
		return list;
	}

	@Override
	@Transactional
	public NormalResult updateByPrimaryKeySelective(DataBase record) {
		NormalResult result = new NormalResult("9999", "操作失败");
		try {
			Map<String,Object> map = new HashMap<>(10);
			map.put("id", record.getId());
			List<DataBase> list = basesmapper.findByCondition(map);
			DataBase b = list.get(0);
			if (record.getValue_() != b.getValue_()) {
				/* 先新增一条基础数据 */
				b.setId(StringUtil.createUUID());
				b.setCjr(record.getXgr());
				b.setSxsj(new Date());
				b.setSfyx("2");
				basesmapper.insertSelective(b);
				/* 再修改这条数据 */
				record.setCjsj(new Date());
				basesmapper.updateByPrimaryKeySelective(record);
				result.setCode("0000");
				result.setMessage("操作成功");
			} else {
				result.setCode("0000");
				result.setMessage("原值与新值并无变化,无须修改");
				return result;
			}
		} catch (Exception e) {
			result.setCode("9999");
			result.setDataset(e);
			result.setMessage("修改失败");
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();// 手动回滚
		}
		return result;
	}

	@Override
	public int count(Map<String,Object> map) {
		int total = 0;
		try {
			total = basesmapper.count(map);
		} catch (Exception e) {
			logout.error(e);
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();// 手动回滚
		}
		return total;
	}

	@Override
	public Boolean whetherCloseAccount() {
		Boolean flag = false;
		try {
			List<DataBase> list = basesmapper.getCloseAccount();
			if (list.size() > 0) {
				Long time = System.currentTimeMillis();
				Long sxTime = (list.get(0).getSxsj()).getTime();
				if (time < sxTime) {
					flag = true;
				}
			}
		} catch (Exception e) {
			logout.error(e);
		}
		return flag;
	}

	@Override
	public NormalResult updateCloseAccount(DataBase record, Integer status) {
		NormalResult result = new NormalResult("9999", "操作失败");
		if (status == 0) {
			record.setCjsj(new Date());
			Map<String, HttpSession> map = Tools.SESSIONS;
			HttpSession session = map.get("admin");
			Tools.SESSIONS.clear();
			Tools.SESSIONS.put("admin", session);
		} else {
			record.setSxsj(new Date());
		}
		try {
			basesmapper.updateAccount(record);
			result.setCode("0000");
			result.setMessage("操作成功");
		} catch (Exception e) {
			logout.error(e);
		}
		return result;
	}

	@Override
	public List<DataBase> getCloseAccount() {
		List<DataBase> list = new ArrayList<DataBase>();
		try {
			list = basesmapper.getCloseAccount();
		} catch (Exception e) {
			logout.error(e);
		}
		return list;
	}

}
